﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Drawing.Drawing2D;

namespace THO7_Team8
{
	//internal class Greyscale : VisionAlgorithm
    class Greyscale : VisionAlgorithm
	{
		
        private Size bitmapSize;
        public Greyscale(String name) : base(name) { }
        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {

            Bitmap returnImage = new Bitmap(sourceImage);
            bitmapSize = returnImage.Size;
            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, returnImage.Width, returnImage.Height);
            BitmapData bmpData =
                returnImage.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                PixelFormat.Format24bppRgb);
            int hoogte = bitmapSize.Height;
            int breedte = bitmapSize.Width;
            unsafe
            {
                int pixelSize = 3;
                for (int y = 0; y < hoogte; y++)
                {
                    byte* row = (byte*)bmpData.Scan0 + (y * bmpData.Stride);
                    for (int x = 0; x < breedte; x++)
                    {
                        //create the greyscale version
                        // Formula: Wikipedia gray_scale
                        byte greyScale =
                           (byte)((row[x * pixelSize] * .11) + // Blue
                           (row[x * pixelSize + 1] * .59) +  // Green
                           (row[x * pixelSize + 2] * .3)); // Red
                        //set the new image's pixel to the greyscale version
                        row[x * pixelSize] = greyScale; // Blue
                        row[x * pixelSize + 1] = greyScale; // Green
                        row[x * pixelSize + 2] = greyScale; // Red
                    }
                }
            }


            // Unlock the bits.
            returnImage.UnlockBits(bmpData);

            return returnImage;
        }

        //public Greyscale(String name)
		//	: base(name)
		//{
		//}

        //public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap img)
        //{
        //    Bitmap newBitmap = (Bitmap)img.Clone();
        //    //create an empty bitmap the same size as original
        //    BitmapData newBitmap_Data = newBitmap.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, img.PixelFormat);

        //    // Get the address of the first line.
        //    IntPtr ptr = newBitmap_Data.Scan0;

        //    // Declare an array to hold the bytes of the bitmap.
        //    // This code is specific to a bitmap with 24 bits per pixels.
        //    int bytes = Math.Abs(newBitmap_Data.Stride) * newBitmap.Height;
        //    byte[] rgbValues = new byte[bytes];

        //    // Copy the RGB values into the array.
        //    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

        //    int clr = 0;
        //    int B = 2;
        //    int R = 3;
        //    int G = 4;

        //    int NextPixelColor = 2;
        //    int counter = 0;
        //    for (counter = 0; counter <= (rgbValues.Length - 1); counter += 1) {
        //        switch (NextPixelColor) {
        //            case 2:
        //                clr = (int)(0.2 * rgbValues[counter]);
        //                NextPixelColor = R;
        //                break;
        //            case 3:
        //                clr = clr + (int)(0.59 * rgbValues[counter]);
        //                NextPixelColor = G;
        //                break;
        //            case 4:
        //                clr = clr + (int)(0.11 * rgbValues[counter]);
        //                //clear is de waarde van 0.2*blue + 0.59*red + 0.11*green. Deze waarde wordt nu gebruikt
        //                rgbValues[counter - 2] = (byte)clr;
        //                //om alle oude kleurwaardes te vervangen door de nieuwe verkregen grijswaarde.
        //                rgbValues[counter - 1] = (byte)clr;
        //                rgbValues[counter] = (byte)clr;
        //                NextPixelColor = B;
        //                break;
        //        }
        //    }
        //    //Console.WriteLine("Pixelcounter: " + counter.ToString() + " rgb.length: " + rgbValues.Length.ToString())
        //    // Copy the RGB values back to the bitmap
        //    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

        //    //unlock the bitmaps
        //    newBitmap.UnlockBits(newBitmap_Data);
            
        //    return newBitmap;
        //}

        //public static int[] MakeGrey(int[] orginalArray)
        //{
        //    int size = orginalArray.Length;
        //    //Modified array for return
        //    int[] modifiedArray = new int[size];

        //    //Loops through all the pixels in the image
        //    for(int i = 0; i < size; i++)
        //    {
        //        //int for greycolor
        //        int grey = 0;

        //        //gets the greycolor
        //        grey += orginalArray[i] & 255;
        //        grey += (orginalArray[i] >> 8) & 255;
        //        grey += (orginalArray[i] >> 16) & 255;
        //        grey /= 3;

        //        //sets the greycolor in the modified array
        //        modifiedArray[i] = 255 << 24 | (Convert.ToByte(grey) << 16) | (Convert.ToByte(grey) << 8) | Convert.ToByte(grey);
        //    }
        //    return modifiedArray;
        //}
	}
}